<?php
namespace App\Traits;


use App\Http\Controllers\Api\Serializer\CustomSerializer;
use Dingo\Api\Routing\Helpers;

trait Responder
{

    use Helpers;

    public function responseCollection($collection,  $transformer)
    {
        return $this->response->collection($collection, $transformer, [], function ($resource,  $fractal) {
            $fractal->setSerializer(new CustomSerializer());
        });
    }

    public function responseItem($item,  $transformer)
    {
        return $this->response->item($item, $transformer, [], function ($resource,  $fractal) {
            $fractal->setSerializer(new CustomSerializer());
        });
    }

    public function responsePaginate($paginator,  $transformer, $meta=[])
    {
        return $this->response->paginator($paginator, $transformer, [], function ($resource, $fractal) {
            $fractal->setSerializer(new CustomSerializer());
        })->setMeta($meta);
    }

     /*
     * error resposne
     * @param message
     * @param status
     */

    protected function errorResponse(  $message = "不能为空", $status_code = 406, $data=[]  ) {
        
        $response = array(
            'message' => $message,
            'code' => $status_code,
        );
        return response()->json(array_merge($response,$data));
    }

    /*
     * success response
     * @param message
     * @param status
     */

    protected function successResponse($message = "请求成功", $data=[], $status_code = 200) {
        $response = array(
            'message' => $message,
            'code' => $status_code,
        );
        return response()->json(array_merge($response,$data));
    }
}